package cz.semjobKB.triples.api.impl;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cz.semjobKB.conllx.api.ISentence;
import cz.semjobKB.conllx.api.IWord;
import cz.semjobKB.conllx.node.EWordType;

public class NounStats {
		
		HashMap<String, Integer> mergeConstituent = new HashMap<String, Integer>();
		
		HashMap<String, Integer> mergeMorpho = new HashMap<String, Integer>();
		
		public String processFrazes(List<ISentence> sentences) {
			String result = "";
			for (ISentence sentence : sentences) {
				for (IWord word : sentence.getWords()) {
					if (word.getWordType() != EWordType.NOUN) {
						continue;
					}
					if (mergeConstituent.containsKey(word.getLemma())) {
						mergeConstituent.put(word.getLemma(), mergeConstituent.get(word.getLemma()) + 1);
					} else {
						mergeConstituent.put(word.getLemma(), 1);
					}
				}
			}		
			mergeConstituent = sortByValue(mergeConstituent);
			
			for (Entry<String, Integer> entry : mergeConstituent.entrySet()) {
				result += entry.getKey()+ "\t" + entry.getValue().toString() + "\n";
			}
			result += "\n\n\n";
						
			return result;
			
		}
		
		public static <K, V extends Comparable<? super V>> HashMap<K, V> sortByValue(Map<K, V> map) 
		{
		    @SuppressWarnings("unchecked")
		    Map.Entry<K,V>[] array = map.entrySet().toArray(new Map.Entry[map.size()]);

		    Arrays.sort(array, new Comparator<Map.Entry<K, V>>() 
		    {
		        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) 
		        {
		            return e2.getValue().compareTo(e1.getValue());
		        }
		    });

		    HashMap<K, V> result = new LinkedHashMap<K, V>();
		    for (Map.Entry<K, V> entry : array)
		        result.put(entry.getKey(), entry.getValue());

		    return result;
		}
	

}
